package com.example.wangzh.developkit.adapter;

import android.app.Activity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.wangzh.developkit.widget.GalleryHandler;

import java.util.Arrays;

/**
 * Created by USER on 2017/5/27.
 */

public class GlideLoadAdapter extends PagerAdapter {
    Activity activity;
    String[] imgUrls;
    GalleryHandler galleryHandler;
    int blankImgRes;

    public GlideLoadAdapter(Activity activity, String[] imgUrls, GalleryHandler galleryHandler, int blankImgRes) {
        this.activity = activity;
        this.imgUrls = imgUrls;
        this.galleryHandler = galleryHandler;
        this.blankImgRes = blankImgRes;
    }


    @Override
    public int getCount() {
        return imgUrls.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public View instantiateItem(ViewGroup container, final int position) {

        ImageView imageView = new ImageView(activity);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == galleryHandler) {
                    throw new NullPointerException("galleryHandler为空");
                } else {
                    galleryHandler.showBigPic(Arrays.asList(imgUrls), position);
                }
            }
        });
        container.addView(imageView, ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.MATCH_PARENT);
        Glide.with(activity).load(imgUrls[position]).error(blankImgRes)
                .into(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
